﻿using AutoMapper;
using IRepository;
using IServices.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using ViewModel.Response;

namespace Services.Base
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        public IBaseRepository<TEntity> baseRepository;
        protected IMapper mapper;
        public BaseService(IMapper mapper)
        {
            this.mapper = mapper;
        }
        public int Add(TEntity entity)
        {
            return baseRepository.Add(entity);
        }

        public bool Delete(TEntity model)
        {
            return baseRepository.Delete(model);
        }

        public bool DeleteById(object id)
        {
            return baseRepository.DeleteById(id);
        }

        public bool Delete(Expression<Func<TEntity, bool>> whereExpression)
        {
            return baseRepository.Delete(whereExpression);
        }

        public IEnumerable<TEntity> Query(bool isNoTracking = true)
        {
            return baseRepository.Query(isNoTracking);
        }

        public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, bool isNoTracking = true)
        {
            return baseRepository.Query(whereExpression, isNoTracking);
        }

        public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true, bool isNoTracking = true)
        {
            return baseRepository.Query(whereExpression, orderByExpression, isAsc, isNoTracking);
        }

        public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, Expression<Func<TEntity, object>> orderByExpression = null, bool isAsc = true, bool isNoTracking = true)
        {
            return baseRepository.Query(whereExpression, intTop, orderByExpression, isAsc, isNoTracking);
        }

        public IEnumerable<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, Expression<Func<TEntity, object>> orderByExpression = null, bool isAsc = true, bool isNoTracking = true)
        {
            return baseRepository.Query(whereExpression, intPageIndex, intPageSize, orderByExpression, isAsc, isNoTracking);
        }

        public TEntity QueryById(object Id)
        {
            return baseRepository.QueryById(Id);
        }

        public bool Update(TEntity entity, IEnumerable<string> updatePropertyList = null)
        {
            return baseRepository.Update(entity, updatePropertyList);
        }

        public PageModel<IEnumerable<TEntity>> CreatePage(IEnumerable<TEntity> entities, int pageSize, int pageIndex)
        {
            return new PageModel<IEnumerable<TEntity>>
            {
                Data = entities.Skip((pageIndex - 1) * pageSize).Take(pageSize).AsEnumerable(),
                Count = entities.Count(),
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
